package humynn.engine
{
	import flash.display.Stage;
	import flash.events.MouseEvent;

	/**
	 * Used to pass information about input from the system to the
	 * Entities and Controls.
	 */
	public class Input
	{
		public var mouse:Mouse;
		
		private var _lastLMB:Boolean;
		private var _clicks:int;
		
		public function get clicks():int
		{
			return _clicks;
		}
		
		/**
		 * The constructor wires the events so that the input object
		 * can stay up to date.
		 * 
		 * @param target The Stage object to collect the mouse events from
		 */
		public function Input(target:Stage)
		{
			mouse = new Mouse();
			target.addEventListener(MouseEvent.MOUSE_MOVE, mouseEvent);
			target.addEventListener(MouseEvent.MOUSE_DOWN, mouseEvent);
			target.addEventListener(MouseEvent.MOUSE_UP, mouseEvent);
			
			_lastLMB = false;
			_clicks = 0;
		}
		
		/**
		 * Resets the number of clicks to zero.
		 */
		public function resetClicks():void
		{
			_clicks = 0;
		}
		
		/**
		 * Stores mouse information when a mouse event occurs.
		 * 
		 * @param e The event data.
		 */
		private function mouseEvent(e:MouseEvent):void
		{
			mouse.x = e.stageX;
			mouse.y = e.stageY;
			mouse.lmbutton = e.buttonDown;
			
			if(mouse.lmbutton && !_lastLMB)
			{
				_clicks++;
			}
			
			_lastLMB = mouse.lmbutton;
		}
	}
}